package com.dc.service.impl;

import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.dao.UserDao;
import com.dc.model.User;
import com.dc.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    public BaseModel selectPageUser(Integer curr_page, Integer page_size, BaseModel baseModel,String account,String name) {
        // 编写业务
        PageHelper.startPage(curr_page,page_size);
        // 开启分页以后需要马上接入查询语句，方便pageHelper实现分页
        List<User> users=userDao.selectPageUser(account,name);
        PageInfo pageInfo=new PageInfo(users,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("分页查询用户成功！");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel login(String account, String password, BaseModel baseModel) throws Exception {
        User user=userDao.selectUserByAccountAndPwd(account,password);
        if(user == null){
            // 没有查到数据
            throw  new BusinessException(ErrorMessageEnum.LOGIN_ERROR);
        }
            baseModel.setResultCode(0);
            baseModel.setMessage("登录成功！");
            user.setPassword(null);
            baseModel.setData(user);
        return baseModel;
    }

    public BaseModel addUser(User user, BaseModel baseModel) throws Exception {
        // 判断是否本花卉已经添加
        Integer userS = userDao.selectUserByAccount(user.getAccount());
        if(userS>0){
            // 有花卉不准插入
            throw new BusinessException(ErrorMessageEnum.REPEAT_CLIENT);
        }
        // 同名账号不能插入
        Integer userCount=userDao.selectUserByAccount(user.getAccount());
        if(userCount>0){
            // 有账号不准插入
            throw new BusinessException(ErrorMessageEnum.REPEAT_ACCOUNT);
        }
        //调用新增用户的dao
        Integer result=userDao.insertUser(user);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_USER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增用户成功！");
        return baseModel;
    }

    public BaseModel updateUser(User user, BaseModel baseModel) throws Exception {
        // 不能修改账号
        user.setAccount(null);
        // 调用修改用户的dao
        Integer result=userDao.updateUser(user);
        if(result<=0){
            throw  new BusinessException(1008,"修改用户失败");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改用户成功！");

        return baseModel;
    }

    public BaseModel deleteUser(String ids, BaseModel baseModel) throws Exception {
        String[] idArry=ids.split(",");
        int result=userDao.deleteUser(idArry);
        if(result<=0){
            throw new BusinessException(1009,"删除"+result+"个用户失败。");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"个成功！");
        return baseModel;
    }

    public BaseModel impUserDTO(BaseModel baseModel) throws Exception {

        return null;
    }
}
